
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//#include "StdAfx.h"
#include "PreDefine.h"
//////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////// 
 
#include "GameObjectList.h"
#include "../GameObject.h"

GameObjectList::GameObjectList()
: m_objects(0), m_nObjCount(0), m_nMaxCount(0)
{
}

GameObjectList::~GameObjectList()
{
	SAFE_DELETE_ARRAY(m_objects);
}

void 
GameObjectList::Reserve(int count)
{
	ASSERT(!m_objects);
	m_objects = GL_NEW GameObject*[count];
	m_nMaxCount = count;
}

void 
GameObjectList::AddObject(GameObject* obj)
{
	ASSERT( m_nObjCount < m_nMaxCount );

	for( int i = m_nObjCount - 1; i >= 0; i-- )
	{
#if defined(TARGET_IPHONE_SIMULATOR) || defined(TARGET_OS_IPHONE)
#else
		/*if( obj->m_objectID == m_objects[i]->m_objectID )
		{
			DEBUG_OUT( "!!!!!!!!!!!! Error GameObjectList::AddObject Id: %d\n", obj->m_objectID );
		}*/
#endif

		if( obj->m_objectID > m_objects[i]->m_objectID )
		{
			m_objects[i + 1] = obj;
			m_nObjCount++;
			return;
		}
		else
		{
			m_objects[i + 1] = m_objects[i];
		}
	}

	m_objects[0] = obj;
	m_nObjCount++;

	return;
}

void 
GameObjectList::RemoveObject(GameObject* obj)
{
	if (!obj) return;

	int pos = FindObject(obj);

	if ( pos >= 0 )
	{	
		//SAFE_DELETE(obj);
		for( int i = pos; i < m_nObjCount - 1; i++ )
		{
			m_objects[i] = m_objects[i + 1];
		}
		m_nObjCount--;
	}
}

int 
GameObjectList::FindObject(GameObject* obj)
{
	return FindPosBinary( obj->m_objectID, 0, m_nObjCount - 1 );
}

GameObject*
GameObjectList::FindObject(int id)
{
	return FindObjectBinary( id, 0, m_nObjCount - 1 );
}


void
GameObjectList::Clear()
{
	m_nObjCount = 0;
}

int
GameObjectList::GetCount() const
{
	return m_nObjCount;
}

int
GameObjectList::GetMaxCount() const
{
	return m_nMaxCount;
}

GameObject*
GameObjectList::operator[](int i)
{
	return m_objects[i];
}

GameObject*
GameObjectList::FindObjectBinary(int id, int start, int end)
{
	if (start <= end)
	{
		int mid = (start + end)>>1;

		if (m_objects[mid]->m_objectID == id)
		{
			return m_objects[mid];
		}
		else if (id < m_objects[mid]->m_objectID)
		{
			return FindObjectBinary(id, start, mid - 1);
		}
		else
		{
			return FindObjectBinary(id, mid + 1, end);
		}
	}
	else
	{
		return NULL;
	}
}

int
GameObjectList::FindPosBinary(int id, int start, int end)
{
	if (start <= end)
	{
		int mid = (start + end)>>1;

		if (m_objects[mid]->m_objectID == id)
		{
			return mid;
		}
		else if (id < m_objects[mid]->m_objectID)
		{
			return FindPosBinary(id, start, mid - 1);
		}
		else
		{
			return FindPosBinary(id, mid + 1, end);
		}
	}
	else
	{
		return -1;
	}
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------------------------------------------------

